﻿// This code is released under the BSD license.
namespace PugLib.UnitTest
{
    #region

    using System;
    using System.Globalization;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    #endregion

    /// <summary>
    ///This is a test class for SafeConvertTest and is intended
    ///to contain all SafeConvertTest Unit Tests
    ///</summary>
    [TestClass]
    public class SafeConvertTest
    {
        /// <summary>
        ///A test for ToString
        ///</summary>
        [TestMethod]
        public void ToStringTest1()
        {
            const object value = null;
            string expected = string.Empty;
            string actual = SafeConvert.ToString(value);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ToStringTest()
        {
            const double obj = 17.0;
            string expected = 17.ToString(CultureInfo.InvariantCulture);
            string actual = SafeConvert.ToString(obj);
            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for ToShortDateTimeString
        ///</summary>
        [TestMethod]
        public void ToShortDateTimeStringTest()
        {
            SafeConvert.ToShortDateTimeString(new DateTime(1982, 8, 27));
        }

        /// <summary>
        ///A test for ToLongDateTimeString
        ///</summary>
        [TestMethod]
        public void ToLongDateTimeStringTest()
        {
            SafeConvert.ToLongDateTimeString(new DateTime(1982, 8, 27));
        }

        /// <summary>
        ///A test for ToInt64
        ///</summary>
        [TestMethod]
        public void ToInt64Test()
        {
            object value = Int64.MaxValue;
            const long expected = Int64.MaxValue;
            long actual = SafeConvert.ToInt64(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToInt32FromHex
        ///</summary>
        [TestMethod]
        public void ToInt32FromHexTest()
        {
            const string value = "33B";
            const int expected = 827;
            int actual = SafeConvert.ToInt32FromHex(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToInt32
        ///</summary>
        [TestMethod]
        public void ToInt32Test1()
        {
            const object input = null;
            const int expected = 0;
            int actual = SafeConvert.ToInt32(input, 0);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToInt32
        ///</summary>
        [TestMethod]
        public void ToInt32Test()
        {
            object value = 324;
            const int expected = 324;
            int actual = SafeConvert.ToInt32(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToInt16
        ///</summary>
        [TestMethod]
        public void ToInt16Test()
        {
            const object value = null;
            const short expected = 0;
            short actual = SafeConvert.ToInt16(value);
            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for ToGeneralDateString
        ///</summary>
        [TestMethod]
        public void ToGeneralDateStringTest()
        {
            DateTime now = DateTime.UtcNow;
            object expected = now.ToString("G", CultureInfo.InvariantCulture);
            string actual = SafeConvert.ToGeneralDateString(now);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToDouble
        ///</summary>
        [TestMethod]
        public void ToDoubleTest1()
        {
            const object value = null;
            const double expected = 0F;
            double actual = SafeConvert.ToDouble(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToDouble
        ///</summary>
        [TestMethod]
        public void ToDoubleTest()
        {
            const object input = null;
            const double def = 0F;
            const double expected = 0F;
            double actual = SafeConvert.ToDouble(input, def);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToDecimal
        ///</summary>
        [TestMethod]
        public void ToDecimalTest1()
        {
            const object value = null;
            const decimal expected = new Decimal();
            decimal actual = SafeConvert.ToDecimal(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToDecimal
        ///</summary>
        [TestMethod]
        public void ToDecimalTest()
        {
            const object input = null;
            const decimal def = new Decimal();
            const decimal expected = new Decimal();
            decimal actual = SafeConvert.ToDecimal(input, def);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToDateTime
        ///</summary>
        [TestMethod]
        public void ToDateTimeTest()
        {
            const object value = null;
            DateTime expected = new DateTime();
            DateTime actual = SafeConvert.ToDateTime(value);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToBoolean
        ///</summary>
        [TestMethod]
        public void ToBooleanTest1()
        {
            const object input = null;
            const bool def = false;
            const bool expected = false;
            bool actual = SafeConvert.ToBoolean(input, def);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ToBoolean
        ///</summary>
        [TestMethod]
        public void ToBooleanTestTrue()
        {
            const bool expected = true;
            bool actual = SafeConvert.ToBoolean("true", true);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean("True", true);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean("1", true);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ToBooleanTestFalse()
        {
            const bool expected = false;
            bool actual = SafeConvert.ToBoolean("false", false);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean("False", false);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean("FAlse", false);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean("0", false);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean(null, false);
            Assert.AreEqual(expected, actual);

            actual = SafeConvert.ToBoolean(string.Empty, false);
            Assert.AreEqual(expected, actual);
        }


        [TestMethod]
        public void GetAsTest()
        {
            const int value = 17;
            const double expected = 17.0;
            double actual = SafeConvert.GetAs<double>(value);
            Assert.AreEqual(expected, actual);
        }
    }
}